package com.zy.design.pattern.composite;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author zhaoyi
 * @date 2021/4/29 18:08
 * 文件系统
 */

public abstract class IFileSystemNode {

    private String fileName;

    private List<IFileSystemNode> list;


    public IFileSystemNode(String fileName, IFileSystemNode parent) {
        if (parent != null && FileNode.class.equals(parent.getClass())) {
            throw new IllegalArgumentException("父级非目录");
        }
        this.fileName = fileName;
        if (parent != null) {
            List<IFileSystemNode> list = parent.getList();
            if (list == null) {
                list = Lists.newArrayList();
                parent.setList(list);
            }
            list.add(this);
        }
    }


    //获取名称
    public String getFileName() {
        return fileName;
    }

    public List<IFileSystemNode> getList() {
        return list;
    }

    public void setList(List<IFileSystemNode> list) {
        this.list = list;
    }

    public void printAll(int level) {
        for (int i = 0; i < level; i++) {
            System.out.print("--");
        }
        System.out.println("| "+fileName);

        List<IFileSystemNode> list = getList();
        if (CollectionUtil.isNotEmpty(list)) {
            for (IFileSystemNode f : list) {
                f.printAll(level + 1);
            }
        }


    }

}
